<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:output method="xml" media-type="text/plain"/>
	<xsl:template match="/">
		<busMatrix>
			<xsl:for-each select="//SubjectArea">
				<SubjectArea>
					<xsl:copy-of select="SubjectAreaName"/>
					<xsl:variable name="subjectArea" select="SubjectAreaName"/>
					<xsl:copy-of select="SubjectAreaID"/>
					<xsl:variable name="bizModel" select="BusinessCatalogName"/>
					<xsl:copy-of select="BusinessCatalogName"/>
					<xsl:copy-of select="BusinessCatalogID"/>
					<LogicalTableIDList>
						<xsl:for-each select="//BusinessCatalog[./BusinessCatalogID = $bizModel]">
							<xsl:for-each select="LogicalTableIDList/LogicalTableID">
								<xsl:variable name="logicalTableName" select="."/>
								<xsl:if test="count(//SubjectArea/LogicalTableIDList/LogicalTableID[. = $logicalTableName  and ../../SubjectAreaName = $subjectArea]) > 0">
									<xsl:copy-of select="."/>
								</xsl:if>
							</xsl:for-each>
						</xsl:for-each>
					</LogicalTableIDList>
				</SubjectArea>
			</xsl:for-each>
			<!-- copying all logical joins (simplified view) -->
			<xsl:copy-of select="//LogicalJoin"/>
		</busMatrix>
	</xsl:template>
</xsl:stylesheet>
